import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);
// 固定路由
export const constantRoutes = [
  {
    path: "/about",
    component: () => import("@/views/AboutView"),
  },
  {
    path: "/reward/list",
    name: "rewardList",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "reward" */ "../views/reward/list.vue"),
  },
  {
    path: "/reward/package",
    name: "rewardPackage",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "reward" */ "../views/reward/package.vue"),
  },
];

// 创建 路由 对象
const createRouter = () =>
  new Router({
    mode: "history",
    base: window.__POWERED_BY_QIANKUN__ ? "/app-vue/" : "/",
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};

// 动态路由更新 see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
